package com.topdt.jbpm.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.model.Flow;
import com.topdt.jbpm.model.FlowDetail;

/**
 *
 * 建立日期 : 2013-7-19 上午10:00:37<br>
 * 作者 : 武海峰<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class FlowDetailDao extends PaginationDao<FlowDetail> {

  @SuppressWarnings("unchecked")
  public Flow getById(String flowId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "flow.companyId='" + comId + "' and ";
    String hql = "From Flow flow where " + condition + "flow.flowId = ?";
    List<Flow> list = createQuery(hql, new Object[] {flowId}).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public FlowDetail getDetailById(String flowId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "flowDetail.companyId='" + comId + "' and ";
    String hql = "From FlowDetail flowDetail where " + condition + "flowDetail.flowId = ?";
    List<FlowDetail> list = createQuery(hql, new Object[] {flowId}).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public List<FlowDetail> findByFlowId(String flowId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "fd.companyId='" + comId + "' and ";
    String hql =
        "from FlowDetail fd  where " + condition + "fd.flowId = ? order by fd.createTime desc";
    List<FlowDetail> list = createQuery(hql, new Object[] {flowId}).list();
    return list;
  }

  @SuppressWarnings("unchecked")
  public FlowDetail findById(String detailId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "fd.companyId='" + comId + "' and ";
    String hql = "from FlowDetail fd where " + condition + "fd.detailId = ?";
    List<FlowDetail> list = createQuery(hql, new Object[] {detailId}).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public String findMaxNum(String flowId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "company_id='" + comId + "' and ";
    // cast(version as DECIMAL)兼容mysql和oracle
    String sql =
        "select version from wf_flow_detail where " + condition
            + "flow_id = ? order by cast(version as DECIMAL) desc";
    List<String> list = createSqlQuery(sql, new Object[] {flowId}).list();
    if (list != null && list.size() > 0) {
      return String.valueOf(list.get(0));
    }
    return "0";
  }

  @SuppressWarnings("unchecked")
  public List<FlowDetail> getVersionByFlowId(String flowId) {
    String comId = AppContext.getCurrentComId();
    String condition = "";
    if (comId != null && !"".equals(comId))
      condition = "companyId='" + comId + "' and ";

    String hql =
        "from FlowDetail where " + condition
            + " flowId = ?  and jbpmFlowId is not null  order by version asc";
    List<FlowDetail> list = createQuery(hql, new Object[] {flowId}).list();
    return list;
  }

  public void addFlowDetail(String companyId) throws HibernateException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String sysDateString = sdf.format(new Date());
    String sql =
        "insert into wf_flow_detail w (w.DETAIL_ID,w.FLOW_ID,w.CREATE_TIME,w.STATE,w.VERSION,w.JBPM_FLOW_ID,w.JBPM_FLOW_KEY,w.XML,w.JSON,w.COMPANY_ID) "
            + " select :companyId||wf.DETAIL_ID,:companyId||wf.FLOW_ID,TO_DATE('"
            + sysDateString
            + "','YYYY/MM/DD HH24:MI:SS'),wf.STATE,wf.VERSION,wf.JBPM_FLOW_ID,wf.JBPM_FLOW_KEY,wf.XML,wf.JSON,:companyId "
            + " from sys_wf_flow_detail wf";
    SQLQuery sqlQ = (SQLQuery) createSqlQuery(sql).setString("companyId", companyId);
    sqlQ.executeUpdate();
  }

  public void deleteByFlowId(String flowId) {
    Query query = createQuery("delete from FlowDetail where flowId=?", new Object[] {flowId});
    query.executeUpdate();
  }

  @SuppressWarnings("unchecked")
  public FlowDetail getDetailByJbpmflowId(String jbpmFlowId) {
    String hql = "from FlowDetail fd where fd.jbpmFlowId = ?";
    List<FlowDetail> list = createQuery(hql, new Object[] {jbpmFlowId}).list();
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }

}
